package ver1.pixel.handler;

import java.awt.image.BufferedImage;

import ver1.util.MDGauss;
import ver1.util.PixelCnt;
import ver1.util.Position;

public class PixelHandler {
	
	public BufferedImage pixel(BufferedImage img,Position ul,Position dr,PixelCnt cnt){
		//get the matrix by cnt
		int ulx=ul.getX();
		int drx=dr.getX();
		int uly=ul.getY();
		int dry=dr.getY();
		int imgWidth=drx-ulx;
		int pixelWidth=cnt.getCntX();
		int rate=imgWidth/pixelWidth;
		int radius=rate>>1;
		double[][] matrix=MDGauss.get2DMatrix(radius);
		MDGauss.normalize2D(matrix);
		BlurHandler blur=new BlurHandler();
		int pixelHeight=cnt.getCntY();
		BufferedImage newImage=new BufferedImage(pixelWidth,pixelHeight,BufferedImage.TYPE_INT_ARGB);
		for(int x=ulx,newX=0;x+rate-1<=drx&&newX<pixelWidth;x+=rate,newX++){
			for(int y=uly,newY=0;y+rate-1<=dry&&newY<pixelHeight;y+=rate,newY++){
				int pixel=blur.blur(img, x, y, x+rate-1, y+rate-1, matrix);
				newImage.setRGB(newX, newY, pixel);
			}
		}
		return newImage;
	}
	public BufferedImage pixel(BufferedImage img,PixelCnt cnt){
		int width=img.getWidth();
		int height=img.getHeight();
		int pixelWidth=cnt.getCntX();
		int pixelHeight=cnt.getCntY();
		Position ul=new Position(0,0);
		Position dr=new Position(width-1,height-1);
		try {
			cnt=new PixelCnt(ul,dr,pixelWidth,pixelHeight);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pixel(img,ul,dr,cnt);
	} 
}
